package com.student_system.students.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.student_system.students.entity.StudentCourse;
import com.student_system.students.mapper.StudentCourseMapper;
import com.student_system.students.service.IStudentCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student_system.system.entity.SysRoleMenu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljy
 * @since 2025-05-14
 */
@Service
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourse> implements IStudentCourseService {
    @Override
    public List<Integer> getCourseNosByStudentNo(Integer studentNo) {
        // 修改返回值类型为List<Integer>
        QueryWrapper<StudentCourse> Wrapper = new QueryWrapper<>();
        Wrapper.eq("student_no", studentNo);

        List<StudentCourse> studentCourses = this.list(Wrapper);

        List<Integer> courseNos = new ArrayList<>();
        for (StudentCourse studentCourse : studentCourses) {
            courseNos.add(studentCourse.getCourseNo());
        }
        return courseNos;

        //lambda表达式：写法
        // return studentCourses.stream().map(StudentCourse::getCourseNo).collect(Collectors.toList());
    }

    @Override
    public void assignCoursesToStudent(Integer studentNo, List<Integer> courseNos) {
        // 先删除该角色的所有菜单
        QueryWrapper<StudentCourse> Wrapper = new QueryWrapper<>();
        Wrapper.eq("student_no", studentNo);
        this.remove(Wrapper);

        /*//循环保存每一条数据
        for (Integer courseNo : courseNos){
            StudentCourse studentCourse = new StudentCourse();
            studentCourse.setStudentNo(studentNo);  // Long转Integer
            studentCourse.setCourseNo(courseNo);
            this.save(studentCourse);--->批量(放到大括号外面)：this.saveBatch(list);
        }*/

        //批量保存数据到学生课程中间表 lambda表达式
        // 再插入新的菜单
        if (courseNos != null && !courseNos.isEmpty()) {
            //构建新的关联
            List<StudentCourse> list = courseNos.stream()
                    .map(courseNo -> {
                        StudentCourse studentCourse = new StudentCourse();
                        studentCourse.setStudentNo(studentNo);  // Long转Integer
                        studentCourse.setCourseNo(courseNo);            // 保持Integer类型
                        return studentCourse;
                    })
                    .collect(Collectors.toList());
            baseMapper.insert(list);
        }
    }

    @Override
    @Transactional
    public String saveOrUpdateGrade(Integer studentNo, Integer courseNo, Double grade) {
        // 先尝试更新
        LambdaUpdateWrapper<StudentCourse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StudentCourse::getStudentNo, studentNo)
                .eq(StudentCourse::getCourseNo, courseNo)
                .set(StudentCourse::getGrade, grade);

        int updatedRows = this.baseMapper.update(null, updateWrapper);

        if (updatedRows == 0) {
            // 如果没有更新到记录，则插入新记录
            StudentCourse newRecord = new StudentCourse();
            newRecord.setStudentNo(studentNo);
            newRecord.setCourseNo(studentNo);
            newRecord.setGrade(grade);

            if (this.save(newRecord)) {
                return "success";
            } else {
                return "fail";
            }
        }

        return "success";
    }

    @Override
    public List<Map<String, Object>> getGradesByStudentNo(Integer studentNo) throws Exception {
        if (studentNo == null) {
            throw new IllegalArgumentException("学号不能为空");
        }
        List<Map<String, Object>> grades = baseMapper.selectGradesByStudentNo(studentNo);
        return grades;
    }
}
